package com.cy.cykh.app.view.activity

import android.app.Activity
import android.content.DialogInterface
import android.content.Intent
import android.databinding.DataBindingUtil.setContentView
import android.os.Bundle
import android.support.v4.content.ContextCompat.startActivity
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.Toast
import com.cy.cykh.app.cdyapp.R
import com.cy.cykh.app.cdyapp.R.id.*
import com.cy.cykh.app.util.AlertDialogManagerUtil
import com.cy.cykh.app.view.base.BaseActivity
import com.thundersoft.thundersso.plugin.SSOLoginActivity
import com.thundersoft.thundersso.plugin.callback.ClientCallback
import com.thundersoft.thundersso.plugin.callback.SSOCallbackUtils
import kotlinx.android.synthetic.main.activity_login.*

/**
 * @author Administrator
 * @FileName
 * @date 2019/1/2 11:08
 */
class LoginActivity : BaseActivity(), View.OnClickListener, ClientCallback {


    override fun getContentViewID(): Int {
        return 0
    }

    override fun initView() {

    }
    private var loginStatus = -1
    companion object {
        private val TAG = LoginActivity::class.java.simpleName
        @JvmStatic
        fun launch(activity: Activity) {
            val intent = Intent(activity, LoginActivity::class.java)
            activity.startActivity(intent)
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        // 隐藏状态栏
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        setContentView(R.layout.activity_login)
        btn_login.setOnClickListener(this)
        initView()
    }


//        val it =getIntent()
//        val data = it.getExtras()
//        if (data != null) {
//            val startform = data!!.getString("startfrom")
//            LogUtils.i(TAG, "start from = " + startform!!)
//            if ("sso" == startform) {
//                handleLoginResult()
//            }
//        }

    private fun login() {
        val user = edit_user!!.text.toString().trim { it <= ' ' }
        val psw = edit_psw!!.text.toString().trim { it <= ' ' }
        if (user.isEmpty()) {
            Toast.makeText(this, "亲,账号不能为空哦", Toast.LENGTH_SHORT).show()
        } else if (psw.isEmpty()) {
            Toast.makeText(this, "亲,密码不能为空哦", Toast.LENGTH_SHORT).show()
        } else {
            //            if ( !"super".equals(user)){
            //                Toast.makeText(this, "亲,账号输入错误哦", Toast.LENGTH_SHORT).show();
            //            }else if ( !"000".equals(psw)){
            //                Toast.makeText(this, "亲,密码输入错误哦", Toast.LENGTH_SHORT).show();
            //            }else {
            //                Intent intents = new Intent(this, MainActivity.class);
            //                startActivity(intents);
            //                finish();
            SSOCallbackUtils.getInstance().clientCallback = this
            val intents = Intent()
            intents.setClass(this@LoginActivity, SSOLoginActivity::class.java)
            val data = Bundle()
            data.putString("SAPackageName", this.saPackageName)
            data.putString("mode", "sso")
            data.putString("transferData", user)
            data.putString("serverResponseResult", psw)
            intents.putExtras(data)
           startActivity(intents)

        }
    }

    private fun showResult() {
        if (1 == loginStatus) {
            Log.d(TAG, "login success") // 跳转到logSuccess界面
            HomeActivity.launch(this@LoginActivity)
            Log.d(TAG, "showResult finish")
            finish()
        } else { // 登录出错
            AlertDialogManagerUtil.showDialog(this@LoginActivity,
                    "Return login result error, couldn't login", mOnClick)
            }
        }

    private var mOnClick = DialogInterface.OnClickListener { _, _ ->
      AlertDialogManagerUtil.mAlertDialog.dismiss()
        HomeActivity.launch(this@LoginActivity)
        Log.d(TAG, "showResult finish")
        finish()
    }

    // ---------------------------------------------------------------------------------
    override fun handleLoginError(result: String) {
        Log.e("resut", result)
        if ("E0001" == result) {
            AlertDialogManagerUtil.showDialog(this,
                    "单点登录服务器错误", DialogInterface.OnClickListener { _, _  ->
                    AlertDialogManagerUtil.mAlertDialog.dismiss()

            })
        } else if ("E0002" == result) {
            AlertDialogManagerUtil.showDialog(this,
                    "无法启动中电科应用", DialogInterface.OnClickListener {  _, _  ->
                    AlertDialogManagerUtil.mAlertDialog.dismiss()
                    HomeActivity.launch(this@LoginActivity)
                    Log.d(TAG, "showResult finish")
                    finish()
            })


        }
    }

    override fun getSAScheme(): String {
        return "userAgentone"
    }

    override fun getSAPackageName(): String {
        return this.packageName
        // return "com.android.useragentone";
    }

    override fun handleLoginResult() {
        val userName = SSOCallbackUtils.getInstance().loginUsername
        Log.e("登录返回", userName + "")
        if (!TextUtils.isEmpty(userName)) {
            Log.d(TAG, "loginSuccess")
            loginStatus = 1 // 需要解析返回数据，看是否登录
        } else {
            loginStatus = 2
            Log.d(TAG, "Cannot login the idp server")
        }
        showResult()
    }



    override fun onClick(view: View?) {
     login()
//        HomeActivity.launch(this@LoginActivity)
//        finish()
    }
    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy")
    }
}


